package cn.anc.aoniplayer;

import android.util.Log;

import java.util.Locale;

import cn.anc.aonicardv.util.LogUtil;

/**
 * Created by baobaoding on 2017/4/27.
 */

public class AVProcess {
    private static String TAG = "AVProcess";


    /*
        String SD_CARD = Environment.getExternalStorageDirectory() + File.separator;
        String input = SD_CARD + "test.flv";
        String output = SD_CARD + "test.mp4";
        String cmdline = String.format("ffmpeg -i %s %s", input, output);
        AVProcess.execCmdline(cmdline);
        Log.e(TAG, "onCreate: finished");
    */

    private static native void runCmd(int argc, String[] argv);

    private static native int idr2Jpg(byte[] data, int offset, int len, String save_path, int w, int h);

    public static int idr2Jpg(byte[] data, int offset, int len, String save_path) {
//        String cmd = String.format(Locale.US, "ffmpeg -i %s -y -f image2 -t 0.001 -s 384x216 %s",
//                idr_path, save_path);
//        execCmdline(cmd);
        return idr2Jpg(data, offset, len, save_path, 384, 216);
    }

    public static void cut_transcode(String video_path, int start_seconds, int end_seconds, String output_path, String video_resolution) {
        int startMinute = start_seconds / 60;
        int startSecond = start_seconds % 60;
        String format = "ffmpeg -ss 00:%02d:%02d -t %02d -i %s -b:v 2000k -b:a 128k -s 854x480 %s -y";
        String[] args = format.split(" ");

        args[2] = "00:" + startMinute + ":" + startSecond;
        args[4] = String.valueOf(end_seconds - start_seconds);
        args[6] = video_path;
//预留功能，可以截取与原视频分辨率相同的分辨率
//        args[12] = video_resolution;
        args[13] = output_path;

        runCmd(args.length, args);
    }

    public static void strippingAudio(String video_path, String output_path) {
        String format = "ffmpeg -i %s -an -vcodec copy %s -y";
        String[] args = format.split(" ");
        args[2] = video_path;
        args[6] = output_path;
        runCmd(args.length, args);
    }

    public static void replaceVideoSound(String video_path, String audio_path, String output_path) {
        String format = "ffmpeg -i %s -i %s -vcodec copy -shortest %s -y";
        String[] args = format.split(" ");
        args[2] = audio_path;
        args[4] = video_path;
        args[8] = output_path;
        runCmd(args.length, args);
    }

    public static void mixture_video_audio(String audio_path, String video_path, String output_path) {
        String format = "ffmpeg -i %s -i %s -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -f mp4 -vcodec copy %s -y";
        String[] args = format.split(" ");
        args[2] = video_path;
        args[4] = audio_path;
        args[11] = output_path;
        runCmd(args.length, args);
    }

    public static int execCmdline(final String cmd) {
        String[] argv = cmd.split(" ");
        int argc = argv.length;
        runCmd(argc, argv);
        return 0;
    }

    static {
        System.loadLibrary("av-process");
    }
}
